package jnpf.engine.util.xml;

import jnpf.engine.custom.exception.UnSupportExpressionException;

import java.util.List;

/**
 * @description: el表达式工具类
 * @author: duanxu
 * @create: 2025-01-24 12:17
 **/
public class SpElUtil {

    /**
     * 返回是否为嵌套表达式
     *
     * @param s
     * @return
     */
    public static Boolean isComplex(String s) {
        int li = s.indexOf("(");
        int ri = s.indexOf(")");
        if (li == -1 && ri == -1) {
            return false;
        }
        if (li == -1 || ri == -1) {
            throw new UnSupportExpressionException("表达式解析失败: " + s);
        }
        int subStart = li + 1;
        String parenStr = s.substring(subStart, ri);
        if (parenStr.contains("(") || parenStr.contains(")")) {
            return true;
        }
        // 替换表达式
        String replaceStr = "${}";
        return isComplex(s.replace(addParen(parenStr), replaceStr));
    }

    /**
     * 解析简单表达式
     *
     * @param expression
     * @return
     */
    public static List<ProperCond> parseSimpleExpression(String expression) {
        return SimpleSpELToProperCondParser.parseSimpleExpressionToProperCond(expression);
    }

    /**
     * 解析复杂表达式
     *
     * @param expression
     * @return
     */
    public static ComplexCond parseComplexExpression(String expression) {
        return ComplexSpELToProperCondParser.parseUnSimpleExpressionToProperCond(expression);
    }


    private static String addParen(String s) {
        return "(" + s + ")";
    }

}
